package com.miuugo.common.tools;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author shaopeng.hu
 */
public class LocalDateUtil {
    // 01. java.util.Date --> java.time.LocalDateTime
    public static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static LocalDateTime toLocalDateTime(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone);
    }

    // 02. java.util.Date --> java.time.LocalDate
    public static LocalDate toLocalDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate toLocalDate(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone).toLocalDate();
    }

    // 03. java.util.Date --> java.time.LocalTime
    public static LocalTime toLocalTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalTime toLocalTime(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone).toLocalTime();
    }


    // 04. java.time.LocalDateTime --> java.util.Date
    public static Date toDate(LocalDateTime dateTime) {
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDateTime dateTime, ZoneId zone) {
        return Date.from(dateTime.atZone(zone).toInstant());
    }


    // 05. java.time.LocalDate --> java.util.Date
    public static Date toDate(LocalDate date) {
        return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDate date, ZoneId zone) {
        return Date.from(date.atStartOfDay(zone).toInstant());
    }

    // 06. java.time.LocalTime --> java.util.Date
    public static Date toDate(LocalTime time) {
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), time);
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalTime time, ZoneId zone) {
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), time);
        return Date.from(dateTime.atZone(zone).toInstant());
    }

    public static String format(LocalDate date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));
    }

    public static String format(LocalDateTime dateTime, String format) {
        return dateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static LocalDate parse(String dateStr, String format) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format));
    }

    public static LocalDateTime parseTime(String datetimeStr, String format) {
        return LocalDateTime.parse(datetimeStr, DateTimeFormatter.ofPattern(format));
    }
}
